package org.cocos2dx.javascript;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;

public class WDNativePlatform {
    private static WDNativePlatform instance;
    public static WDNativePlatform getInstance(){
        if(instance==null)
        {
            instance = new WDNativePlatform();
        }
        return instance;
    };
    public AppActivity app;
    public void init(AppActivity a, Bundle savedInstanceState)
    {
        app = a;
        MarsWrapper.getInstance().init(a,savedInstanceState);
        Log.d("WDNativePlatform","Init");
    }

    public void onStart() {
        if(app==null)return;
        MarsWrapper.getInstance().onStart();
    }

    public void onRestart() {
        if(app==null)return;
        MarsWrapper.getInstance().onRestart();
    }

    public void onResume() {
        if(app==null)return;
        MarsWrapper.getInstance().onResume();
    }

    public void onPause() {
        if(app==null)return;
        MarsWrapper.getInstance().onPause();
    }

    public void onStop() {
        if(app==null)return;
        MarsWrapper.getInstance().onStop();
    }


    public void onDestroy() {
        if(app==null)return;
        MarsWrapper.getInstance().onDestroy();
    }

    public void onNewIntent(Intent intent) {
        if(app==null)return;
        MarsWrapper.getInstance().onNewIntent(intent);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(app==null)return;
        MarsWrapper.getInstance().onActivityResult(requestCode,resultCode,data);
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if(app==null)return;
        MarsWrapper.getInstance().onRequestPermissionsResult(requestCode,permissions,grantResults);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return MarsWrapper.getInstance().onKeyDown(keyCode,event);
    }

    private static String userName;
    private static String isNew;
    public static void login(String un,String inew) {
        Log.d("WDNativePlatform","login:"+un);
        userName = un;
        isNew = inew;
        WDNativePlatform.getInstance().app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                MarsWrapper.getInstance().login(userName, isNew);
            }
        });
    }

    public static void showAd(){
        Log.d("WDNativePlatform","showAd");
        WDNativePlatform.getInstance().app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                MarsWrapper.getInstance().showVideoAd();
            }
        });
    }

    public static void customEvent(String event){
        //==========>customEvent
        MarsWrapper.getInstance().customEvent(event);
    }
    public static void showFullVideo()
    {
        Log.d("WDNativePlatform","showFullVideo");
        WDNativePlatform.getInstance().app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                MarsWrapper.getInstance().showFullVideoAd();
            }
        });
    }

    public static void showBannerAd()
    {
        Log.d("WDNativePlatform","showBanner ");
        WDNativePlatform.getInstance().app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                MarsWrapper.getInstance().setBannerVisibility();
            }
        });
    }
    public static void hideBannerAd()
    {
        Log.d("WDNativePlatform","hideBanner");
        WDNativePlatform.getInstance().app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                MarsWrapper.getInstance().setBannerInvisibility();
            }
        });
    }
    public static void showFeedAd()
    {
        Log.d("WDNativePlatform","showFeed ");
        WDNativePlatform.getInstance().app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                MarsWrapper.getInstance().setFeedVisibility();
            }
        });
    }
    public static void hideFeedAd()
    {
        Log.d("WDNativePlatform","hdieFeed");
        WDNativePlatform.getInstance().app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                MarsWrapper.getInstance().setFeedInvisibility();
            }
        });
    }

    private static String mParam;
    public static void callJS (String param) {
        mParam = param;
        WDNativePlatform.getInstance().app.runOnGLThread(new Runnable() {
            @Override
            public void run() {
                Cocos2dxJavascriptJavaBridge.evalString("window.wdNativeVideoResult('" + mParam + "')");
            }
        });
    }

    private static String mUrl;
    public static void downloadAPK (String url) {
        mUrl = url;
        WDNativePlatform.getInstance().app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                DownLoadUtils utils = new DownLoadUtils(WDNativePlatform.getInstance().app.getApplicationContext(), mUrl, "mrhbw", "末日捍堡王");
                utils.downloadAPK();
            }
        });
    }
    public static void register(){

    }

    public static void loginSuccess(String jsStr){
      
    }
}
